function t(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

var e = t(require("../../../utils/api2")), i = t(require("../../../utils/util"));

Page({
    offset: 0,
    limit: 10,
    data: {
        toolbarWidth: 350,
        activeIndex: -1,
        noMore: !1
    },
    onLoad: function(t) {
        var e = t.mode;
        "track" === (void 0 === e ? "track" : e) ? (this.loadViewList(), wx.setNavigationBarTitle({
            title: "浏览历史"
        })) : (this.loadFocusList(), wx.setNavigationBarTitle({
            title: "我的关注"
        }));
    },
    loadViewList: function() {
        var t = this, i = !(arguments.length > 0 && void 0 !== arguments[0]) || arguments[0];
        if (this.loading) return Promise.reject();
        this.loading = !0, i ? this.offset = 0 : this.offset += this.limit;
        return e.default.site.site.getviewlists({
            data: {
                offset: this.offset,
                limit: this.limit
            },
            desc: "获取列表",
            complete: function() {
                t.loading = !1, wx.stopPullDownRefresh();
            }
        }).then(function(e) {
            var s = e.lists, a = void 0;
            a = i ? s : t.data.list.concat(s), t.setData({
                list: a,
                noMore: s.length < t.limit
            });
        });
    },
    loadFocusList: function() {
        var t = this, i = !(arguments.length > 0 && void 0 !== arguments[0]) || arguments[0];
        e.default.site.focus.mylists({
            data: {
                offset: this.offset,
                limit: 10
            },
            desc: "加载中"
        }).then(function(e) {
            t.setData({
                list: i ? e.lists : t.data.list.concat(e.lists),
                noMore: e.lists.length < 10
            }), t.offset += e.lists.length;
        });
    },
    touchstart: function(t) {
        if ("track" === this.__query.mode && 1 === t.touches.length) {
            var e = t.currentTarget.dataset.index, i = this.data.activeIndex;
            -1 !== i && e !== i && this.updateSiteStyle(i), this.setData({
                startX: t.touches[0].clientX,
                activeIndex: e
            });
        }
    },
    touchmove: function(t) {
        if ("track" === this.__query.mode && 1 === t.touches.length) {
            var e = t.touches[0].clientX, i = this.data.startX - e, s = this.data.toolbarWidth, a = void 0;
            a = i <= 0 ? 0 : i > 50 ? -s : -i;
            var o = t.currentTarget.dataset.index, l = this.data.list;
            l[o].styleleft = a, this.setData({
                list: l
            });
        }
    },
    touchend: function(t) {
        if ("track" === this.__query.mode && 1 === t.changedTouches.length) {
            var e = t.changedTouches[0].clientX, i = this.data.startX - e, s = this.data.toolbarWidth, a = void 0;
            a = i > 50 ? -s : 0;
            var o = t.currentTarget.dataset.index, l = this.data.list;
            l[o].styleleft = a, this.setData({
                list: l
            });
        }
    },
    updateSiteStyle: function(t) {
        var e = this.data.list, s = e[t];
        i.default.extend(s, {
            styleleft: 0
        }), this.setData({
            list: e
        });
    },
    calcTopTotal: function() {
        var t = 0;
        return this.data.list.forEach(function(e, i) {
            "1" === e.istop && (t += 1);
        }), t;
    },
    setTopHandle: function(t) {
        var e = this.data.list;
        if ("0" === e[t].istop) {
            var i = e[t];
            i.istop = "1", i.styleleft = 0;
            var s = e.splice(t, 1)[0];
            e.unshift(s), this.setData({
                list: e,
                activeIndex: 0
            });
        } else {
            var a = e[t], o = this.calcTopTotal();
            a.istop = "0", a.styleleft = 0;
            var l = e.splice(t, 1)[0];
            e.splice(o, 0, l), this.setData({
                list: e,
                activeIndex: t
            });
        }
    },
    toggleIsTop: function(t) {
        var i = this, s = t.currentTarget.dataset.index, a = this.data.list[s], o = a.sid, l = "0" === a.istop ? "set" : "cancel";
        e.default.site.home.settop({
            data: {
                sid: o,
                op: l
            },
            desc: "操作"
        }).then(function() {
            i.setTopHandle(s), i.$emit("update:viewlist");
        });
    },
    deleteViewSite: function(t) {
        var i = this, s = t.currentTarget.dataset.index, a = this.data.list, o = a[s].sid;
        e.default.site.home.deleteview({
            data: {
                sid: o
            },
            desc: "删除"
        }).then(function() {
            a.splice(s, 1), i.setData({
                list: a
            }), i.$emit("update:viewlist");
        });
    },
    viewSite: function(t) {
        var e = t.currentTarget.dataset.sid;
        this.$route("/pages/website/index/index?sid=" + e + "&op=view");
    },
    onReachBottom: function() {
        this.data.noMore || ("track" === this.__query.mode ? this.loadViewList(!1) : this.loadFocusList(!1));
    }
});